/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sjtu.bean;

import com.sjtu.constants.Constants;
import com.sjtu.constants.Constants;
import com.sjtu.database.DataBase;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author p w
 */
public class PurchaseRequestBean {

    private int id;
    private String bookName;
    private String comments;
    private int score;
    private String isbn;
    private String author;
    private String publisher;
    private int userId;
    private int adminId;
    private int status;
    private Date createDate;

    public PurchaseRequestBean(int id, String bookName, String comments, int score, String isbn, String author, String publisher, int userId, int adminId, int status, Date createDate) {
        this.id = id;
        this.bookName = bookName;
        this.comments = comments;
        this.score = score;
        this.isbn = isbn;
        this.author = author;
        this.publisher = publisher;
        this.userId = userId;
        this.adminId = adminId;
        this.status = status;
        this.createDate = createDate;
    }

    public int getAdminId() {
        return adminId;
    }

    public void setAdminId(int adminId) {
        this.adminId = adminId;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getBookName() {
        return bookName;
    }

    public void setBookName(String bookName) {
        this.bookName = bookName;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getIsbn() {
        return isbn;
    }

    public void setIsbn(String isbn) {
        this.isbn = isbn;
    }

    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public static PurchaseRequestBean getRequest(int requestId) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from purchaserequest where id = '" + requestId + "';");
            PurchaseRequestBean request = null;
            if (rs.next()) {
                request = setRequest(rs);
            }
            rs.close();
            return request;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    private static PurchaseRequestBean setRequest(ResultSet rs) {
        try {
            if (rs == null) {
                return null;
            }
            return new PurchaseRequestBean(rs.getInt("id"), rs.getString("bookName"),
                    rs.getString("comments"), rs.getInt("score"),
                    rs.getString("isbn"), rs.getString("author"),
                    rs.getString("publisher"), rs.getInt("userId"),
                    rs.getInt("adminId"), rs.getInt("status"),
                    rs.getDate("date"));
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static PurchaseRequestBean addRequest(String bookname, String comments, int score, String isbn,
            String author, String publisher, int userId) {
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from purchaserequest where bookName = '" + bookname + "';");
            if (rs.next()) {
                return PurchaseRequestBean.setRequest(rs);
            }
            Date dateNow = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String addDate = dateFormat.format(dateNow);
            String sql = "insert into purchaserequest (date, status, comments, bookName, score, userId, isbn,author ,publisher) values ('" 
                    + addDate + "'," + Constants.purchaseRequest_SubmitRequest + ",'" + comments + "','" +
                    bookname + "'," + score + "," + userId + ",'" + isbn + "','" + author + "','" + publisher + "');";
//            System.out.println(sql);
            database.update(sql);
            rs = database.query("select * from purchaserequest where bookName = '" + bookname + "';");
//            System.out.println("select * from book where name = '" + bookname + "';");
            if (rs.next()) {
                return PurchaseRequestBean.setRequest(rs);
            }
//            rs.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static boolean checkRequest(int requestId, int status, int userId) {
        try {
            DataBase database = DataBase.getInstance();
            int AdminId = UserBean.getAdminId(userId);
            String sql = "update purchaserequest set status= " + status + " ,adminId = " + AdminId + " where id = " + requestId;
//            System.out.println(sql);
            database.update(sql);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static ArrayList<PurchaseRequestBean> getPurchaseRequest(int reqType) {
        ArrayList<PurchaseRequestBean> request = new ArrayList<PurchaseRequestBean>();
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = null;
            PurchaseRequestBean reqBean;
            if (reqType == 0) {
                rs = database.query("select * from purchaserequest where status =" + Constants.purchaseRequest_SubmitRequest + " ;");
            }
            if (reqType == 1) {
                rs = database.query("select * from purchaserequest where status =" + Constants.purchaseRequest_adminAgreed + " ;");
            }
            if (reqType == 2) {
                rs = database.query("select * from purchaserequest where status =" + Constants.purchaseRequest_adminRejected + " ;");
            }
            if (reqType == 3) {
                rs = database.query("select * from purchaserequest where status =" + Constants.purchaseRequest_adminpostpone + " ;");
            }
            if (reqType == 4) {
                rs = database.query("select * from purchaserequest where status =" + Constants.purchaseRequest_adminsolved + " ;");
            }
            while (rs.next()) {
                request.add(PurchaseRequestBean.setRequest(rs));
            }
            rs.close();
            return request;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static ArrayList<PurchaseRequestBean> getUserRequestList(int UserId) {
        ArrayList<PurchaseRequestBean> request = new ArrayList<PurchaseRequestBean>();
        try {
            DataBase database = DataBase.getInstance();
            ResultSet rs = database.query("select * from purchaserequest where UserId = " + UserId + " ;");
            while (rs.next()) {
                request.add(PurchaseRequestBean.setRequest(rs));
            }
            return request;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
